/**
 * @author yuliang
 *
 */
package com.xuhai.libms.daoImpl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.xuhai.libms.dao.BookDao;
import com.xuhai.libms.entity.Book;

public class BookDaoImpl extends HibernateDaoSupport implements BookDao{

	public Book getBookById(int id) {
		return (Book)super.getHibernateTemplate().get(Book.class, id);
	}

	public void insert(Book book) {
		super.getHibernateTemplate().save(book);
	}

	public void modify(Book book) {
		super.getHibernateTemplate().update(book);
		
	}

	public void remove(Book book) {
		super.getHibernateTemplate().delete(book);
		
	}

	public List<Book> search() {
		return (List)super.getHibernateTemplate().find("from Book");
	}

	public List<Book> showBookByPage(int start,int end,Book book) {
		String name = book.getName();
		String no = book.getNo();
		int type = book.getType();
		String sql = "from Book b where 1=1 ";
		if(name!=null&&name!=""){
			sql +=" and b.name='"+name+"'";
		}
		if(no!=null&&no!=""){
			sql +=" and b.no='"+no+"'";
		}
		if(type!=-1){
			sql +=" and b.type='"+type+"'";
		}
		return (List)super.getHibernateTemplate().find(sql).subList(start, end);
	}

	public int getSize(Book book) {
		String name = book.getName();
		String no = book.getNo();
		int type = book.getType();
		String sql = "from Book b where 1=1 ";
		if(name!=null&&name!=""){
			sql +=" and b.name='"+name+"'";
		}
		if(no!=null&&no!=""){
			sql +=" and b.no='"+no+"'";
		}
		if(type!=-1){
			sql +=" and b.type='"+type+"'";
		}
		System.out.println(sql);
		return super.getHibernateTemplate().find(sql).size();
	}

	public List<Book> getBookByNo(String book_no) {
		String sql = "from Book b where b.no=:no";
		return super.getHibernateTemplate().findByNamedParam(sql, "no", book_no);
	}
	
	
}
